package com.zhiyou100.review.day020.homework;

/**
 * @packageName: Homework05
 * @className: com.zhiyou100.review.day020.homework
 * @Description: TODO 烧饼
 * @author: yang
 * @date: 7/1/20
 */
public class Homework05 {
    public static void main(String[] args) {
        BakedWheatCake bakedWheatCake = new BakedWheatCake();

        Sheller sheller = new Sheller(bakedWheatCake);
        Customer customer = new Customer(bakedWheatCake);

        new Thread(sheller, "武大郎").start();
        new Thread(customer, "西门庆").start();

    }
}

class BakedWheatCake {
    /**
     * num 记录编号
     * b 记录是否有烧饼
     */
    private int num;
    private boolean booleanValue;

    public BakedWheatCake() {

    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public boolean isBooleanValue() {
        return booleanValue;
    }

    public void setBooleanValue(boolean booleanValue) {
        this.booleanValue = booleanValue;
    }

    public BakedWheatCake(int num, boolean booleanValue) {
        this.num = num;
        this.booleanValue = booleanValue;
    }
}

class Sheller implements Runnable {
    /**
     * 商家类
     * <p>
     * bakedWheatCake 类的引用
     */
    private BakedWheatCake bakedWheatCake;

    public BakedWheatCake getBakedWheatCake() {
        return bakedWheatCake;
    }

    public void setBakedWheatCake(BakedWheatCake bakedWheatCake) {
        this.bakedWheatCake = bakedWheatCake;
    }

    public Sheller(BakedWheatCake bakedWheatCake) {
        this.bakedWheatCake = bakedWheatCake;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (bakedWheatCake) {
                if (!bakedWheatCake.isBooleanValue()) {
                    // 没有烧饼，开始做
                    System.out.println(Thread.currentThread().getName() + " 开始做烧饼 ++++++++" + (bakedWheatCake.getNum() + 1));
                    try {
                        Thread.sleep(200);
                        // 休眠
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    bakedWheatCake.setNum(bakedWheatCake.getNum() + 1);
                    System.out.println(Thread.currentThread().getName() + " 做完烧饼 ++++" + bakedWheatCake.getNum());
                    bakedWheatCake.setBooleanValue(true);
                    // 改变烧饼的状态，有烧饼了
                    bakedWheatCake.notifyAll();
                    // 唤醒其他所有等待的线程
                } else {
                    // 有烧饼
                    try {
                        bakedWheatCake.wait();
                        // 停止制作烧饼
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
            try {
                Thread.sleep(50);
            } catch (Exception e) {
            }
        }
    }
}

class Customer implements Runnable {
    public BakedWheatCake getBakedWheatCake() {
        return bakedWheatCake;
    }

    public void setBakedWheatCake(BakedWheatCake bakedWheatCake) {
        this.bakedWheatCake = bakedWheatCake;
    }

    public Customer(BakedWheatCake bakedWheatCake) {
        this.bakedWheatCake = bakedWheatCake;
    }

    /**
     * 顾客类
     * <p>
     * 烧饼引用 记录烧饼
     */
    private BakedWheatCake bakedWheatCake;

    @Override
    public void run() {
        while (true) {
            synchronized (bakedWheatCake) {
                if (bakedWheatCake.isBooleanValue()) {
                    // 有烧饼，开始吃
                    System.out.println(Thread.currentThread().getName() + " 开始吃烧饼 +++ " + bakedWheatCake.getNum());

                    try {
                        Thread.sleep(100);
                        // 休眠
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " 吃完了烧饼  " + bakedWheatCake.getNum());

                    bakedWheatCake.setBooleanValue(false);
                    // 把 烧饼的状态设为 false
                    bakedWheatCake.notifyAll();
                    // 唤醒其他等待的线程

                } else {
                    // 没有烧饼
                    try {
                        bakedWheatCake.wait();
                        // 当前线程等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    }
}